package net.mikoo.seals.api.model.devices;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.Seal;

public class GetListResponse extends BaseResponse {

	private static final long serialVersionUID = 3058601644501826588L;

	private List<DevicesInfo> datas;

	public List<DevicesInfo> getDatas() {
		return datas;
	}

	public void setDatas(List<DevicesInfo> datas) {
		this.datas = datas;
	}
	
	public static GetListResponse valueOf(PageModel<Devices> pm, Map<Long, DevicesTake> takeMap) {
		GetListResponse response = new GetListResponse();
		response.setResult(ResultType.SUCCESS.getValue());
		if(pm == null || pm.getDatas().isEmpty()) {
			response.setDatas(new ArrayList<DevicesInfo>());
			return response;
		}
		
		List<DevicesInfo> devicesInfoList = new ArrayList<DevicesInfo>();
		for(Devices devices : pm.getDatas()) {
			if(devices == null || DevicesStatusEnum.DESERTED.getCode().equals(devices.getDevicesStatus())) {
				continue;
			}
			DevicesInfo devicesInfo = toDevicesInfo(devices);
			
			DevicesTake devicesTake = takeMap.get(devices.getDevicesId());
			if(devicesTake != null && devicesTake.getPerson() != null) {
				devicesInfo.setTakePartyName(devicesTake.getPerson().getName());
			}
			if(devicesInfo != null){
				devicesInfoList.add(devicesInfo);
			}
		}
		response.setDatas(devicesInfoList);
		return response;
	}
	
	public static DevicesInfo toDevicesInfo(Devices devices) {
		
		if(devices == null) {
			throw new IllegalArgumentException("devices");
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DevicesInfo result = new DevicesInfo();
		
		if(devices.getBindingTime() != null) {
			result.setBindTime(sdf.format(devices.getBindingTime()));
		}
		if(devices.getExpireDate() != null) {
			result.setExpireDate(sdf.format(devices.getExpireDate()));
		}
		
		result.setStatus(devices.getDevicesStatus());;
		result.setDevicesStatus(String.valueOf(devices.getDevicesStatus()));
		
		DevicesStatusEnum status = DevicesStatusEnum.getByCode(devices.getDevicesStatus());
		if(status != null) {
			result.setStatusName(status.getMessage());
		}else {
			result.setStatusName("未知状态");
		}
		
		result.setDevicesId(devices.getDevicesId());
		
		if(devices.getInstallTime() != null) {
			result.setInstallTime(sdf.format(devices.getInstallTime()));
		}
		if(devices.getProduceDate() != null) {
			result.setProduceDate(sdf.format(devices.getProduceDate()));
		}
		result.setRemark(devices.getRemark());
		result.setUseTimes(devices.getUseTimes());
		result.setModel(devices.getModel());;
		
		if(devices.getSeal() != null) {
			Seal seal = devices.getSeal();
			result.setSealName(seal.getSealName());
			result.setCustosPartyName(seal.getCustosName());
			if(StringUtils.isNotBlank(seal.getSealName())) {
				result.setIconText(seal.getSealName().substring(0, seal.getSealName().length() < 2 ? 1 : 2));
			}
			result.setSealStatus(seal.getSealStatus());
		}else {
			result.setIconText(devices.getDevicesCode().substring(0, 2));
		}
		result.setDevicesCode(devices.getDevicesCode());
		return result;
	}
	
}
